<template>
	<view>
		<image src="@/static/code.png" class="content"></image>
		<view class="contenta">
			<!-- 返回按钮 -->
			<view class="backBox" @click="backPage" :style="{'top': topDis + 'px' }">
				<image src="../../static/back.png" mode=""></image>
			</view>
			<view class="head">
				<view class="left">
					<p class="titlea">注册</p>
					<p class="title">请输入您的信息注册账号</p>
				</view>
			</view>
			<view class="card">
				<u-form :model="form" ref="uForm" :rules="rules">
					<u-form-item label-width="0rpx" style="margin-top: 20rpx;" prop="institutionName" :required='true'>
						<!-- <u-input v-model="form.institutionName" placeholder="请输入客户名称" /> -->
						<view style="display: flex; align-items: center; padding-left: 45rpx; background:#fbfbfb;height: 116rpx;">
							<u--textarea autoHeight v-model="form.institutionName" placeholder="请输入客户名称" />
						</view>
					</u-form-item>
					<u-form-item label-width="0rpx" style="margin-top: 20rpx;" prop="contactsName" :required='true'>
						<!-- <u-input v-model="form.contactsName" placeholder="请输入联系人姓名" /> -->
						<view style="display: flex; align-items: center; padding-left: 45rpx; background:#fbfbfb;height: 116rpx;">
							<u--textarea autoHeight v-model="form.contactsName" placeholder="请输入联系人姓名" />
						</view>
					</u-form-item>
					<u-form-item label-width="0rpx" @click.native="pushMap"
						style="margin-top: 20rpx;position: relative;background:#fbfbfb;border-radius: 8rpx;"
						prop="institutionAddress" :required='true'>
						<!-- <u-input v-model="form.institutionAddress" placeholder="请选择客户地址" disabled /> -->
						<view style="display: flex; align-items: center; padding-left: 45rpx; background:#fbfbfb;height: 116rpx;">
							<u--textarea autoHeight v-model="form.institutionAddress" placeholder="请选择客户地址" disabled />
							<u-icon style="" slot="right" name="arrow-right" style="background: red;"></u-icon>
						</view>
					</u-form-item>
					<u-form-item label-width="0rpx" style="margin-top: 20rpx;" prop="contactsMobile" :required='true'>
						<!-- <u-input v-model="form.contactsMobile" placeholder="请输入手机号" /> -->
						<view style="display: flex; align-items: center; padding-left: 45rpx; background:#fbfbfb;height: 116rpx;">
							<u--textarea autoHeight v-model="form.contactsMobile" placeholder="请输入手机号" />
						</view>
					</u-form-item>
					<u-form-item label-width="0rpx" style="margin-top: 20rpx;" prop="verCode" :required='true'>
						<view style="display: flex;align-items: center;background: #fbfbfb;height: 110rpx;width: 100%">
							<!-- <u-input placeholder="请输入验证码" v-model="form.verCode" /> -->
							<view
								style="width: 100%; line-height: 110rpx; padding-left: 45rpx; background:#fbfbfb;height: 110rpx; display: flex;align-items: center;">
								<!-- <u--textarea autoHeight  v-model="form.verCode" placeholder="请输入验证码" /> -->
								<input type="number" v-model="form.verCode" placeholder="请输入验证码"
									style="font-size: 30rpx; padding-left: 20rpx;" placeholder-style="color: #ACAFBF" />
							</view>
							<view>
								<u-toast></u-toast>
								<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
								<!-- <u-button @tap="getCode" :customStyle='customStyle'
									style="height: 70rpx;text-align: center;line-height: 70rpx;width: 200rpx;">{{tips}}</u-button> -->
								<view @tap="getCode" class="getcode">{{tips}}</view>
							</view>
						</view>
					</u-form-item>
					<u-form-item label-width="200rpx" style="margin-top: 20rpx;" prop="name">
						<u-upload @afterRead="afterRead" :fileList="fileList" upload-text='点击上传\n医疗机构签章' max-count='1'
							multiple></u-upload>
						<!-- <base-images v-model="item.returnImgs" :disabled="true" /> -->
					</u-form-item>
				</u-form>
			</view>

			<view class="square" :class="next?'nextActive':''" @click="signin">注册</view>
		</view>

	</view>
</template>

<script>
	import BASE_URL from '@/utils/env.js' //引入接口共用地址
	import {
		sigin,
		getCodevalue,
		sendCode
	} from '../../utils/api.js'
	export default {
		data() {
			return {
				topDis: this.$app.globalData.topDis,
				tips: '',
				seconds: 60,
				next: false,
				form: {
					institutionName: '',
					contactsName: '',
					institutionAddress: '',
					contactsMobile: '',
					verCode: '',
					institutionLatitude: '',
					institutionLongitude: '',
					signature: ''
				},
				fileList: [],
				rules: {
					'institutionName': {
						type: 'string',
						required: true,
						message: '请填写客户姓名',
						trigger: ['blur', 'change']
					},
					'contactsName': {
						type: 'string',
						required: true,
						message: '请填写联系人姓名',
						trigger: ['blur', 'change']
					},
					'institutionAddress': {
						type: 'string',
						required: true,
						message: '请选择客户地址',
						trigger: ['blur', 'change']
					},

					'contactsMobile': [{
							required: true,
							message: '请输入电话',
							trigger: 'blur'
						},
						{
							validator: this.checkPhoneNum,
							trigger: 'blur'
						},
					],
					'verCode': {
						type: 'string',
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					},
				},

			}
		},

		onLoad(options) {
			if (options.longitude) {
				this.form.institutionAddress = options.address
				this.form.institutionLatitude = options.latitude
				this.form.institutionLongitude = options.longitude
			}
		},
		watch: {
			form: {
				deep: true,
				handler(newValue) {
					console.log("newValue", newValue)
					let arr = []
					let notArr = [null, undefined, '']
					Object.keys(newValue).map(el => {
						if (!notArr.includes(newValue[el])) {
							arr.push(el)
						}
						if (arr.length == 7) {
							this.next = true
						} else {
							this.next = false;
						}
					})
				}

			},
		},
		methods: {
			backPage() {
				uni.navigateBack({
					delta: 1
				})
			},
			checkPhoneNum(rule, value, callback) {
				var reg = /^1[3456789]\d{9}$/;
				if (reg.test(value)) {
					callback();
				} else {
					callback(new Error('手机号码格式不正确'));
				}
			},

			signin() {
				this.$refs.uForm.validate().then(() => {
					sigin(this.form).then(data => {
						uni.$u.toast('注册成功');
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
					}).catch(err => {
						uni.$u.toast(err.message);
					})
				}).catch(() => {
					// 验证失败
					uni.$u.toast('请填写完整数据!');
				});
				// /miApi/register

			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.form.contactsMobile === '') return uni.$u.toast('请输入手机号')
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				sendCode({
					phone: this.form.contactsMobile
				}).then(res => {
					console.log("获取验证码成功", res)
				}).catch(err => {
					console.log('err:', err)
					uni.$u.toast('倒计时结束后再发送');
				})
			},
			// signin() {
			// 	uni.navigateTo({
			// 		url: '/pages/Signin/Signin'
			// 	})
			// },
			pushMap() {
				console.log("000")
				uni.navigateTo({
					url: '/pages/Map/Map'
				})
			},
			// 新增图片
			async afterRead(event) {
				console.log('event:', event)
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: BASE_URL + '/sysFileInfo/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							'secretFlag': 'Y',
						},
						header: {
							'Content-Type': 'multipart/form-data',
							Authorization: uni.getStorageSync('token')
						},
						success: (res) => {
							console.log('res:', JSON.parse(res.data))
							this.form.signature = JSON.parse(res.data).data.fileId
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100vh;
		background-size: 100% 100%;
		box-sizing: border-box;


	}

	.nextActive {
		opacity: 1 !important;
	}

	.contenta {
		position: absolute;
		top: 150rpx;
		width: 100%;

	}

	.getcode {
		width: 200rpx;
		font-size: 30rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: Regular;
		position: relative;
		color: #3db020;
		background: #fbfbfb;
		position: relative;
		// width: 30%;
		text-align: center;
		display: flex;
		align-items: center;
		border-radius: 0 8rpx 8rpx 0;

	}

	.iconValue {
		position: absolute;
		position: absolute;
		right: 94rpx;
		margin-top: 35rpx;
		margin-left: 60rpx;


	}

	/deep/ .u-input[data-v-113bc24f] {

		background: #fbfbfb;

	}

	/deep/ .u-textarea--disabled.data-v-09988a29 {
		background: #fbfbfb;
	}

	/deep/.u-form-item__body[data-v-067e4733] {
		padding: 0;
	}

	/deep/ .item1 .u-input--radius {
		border-radius: 8rpx 0 0 8rpx;
	}

	/deep/ .u-button--info {
		border: none;
	}

	/deep/ uni-button:after {
		border: none !important;
	}

	.head {
		width: 670rpx;
		height: 232rpx;
		margin: 0 auto 0;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.title {
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC-Regular;
			font-weight: Regular;
			text-align: left;
			color: #3f4151;
			line-height: 48rpx;
			margin-top: 10rpx;
		}

		.titlea {
			font-size: 60rpx;
			font-family: PingFang SC, PingFang SC-Semibold;
			font-weight: bold;
			text-align: left;
			color: #3f4151;
		}

		.img {
			width: 168rpx;
			height: 64rpx;
		}

		.img1 {
			width: 144rpx;
			height: 144rpx;
		}
	}

	.card {
		width: 670rpx;
		background: #FFF;
		border-radius: 20rpx;
		margin: auto;
		box-sizing: border-box;
		padding: 20rpx 30rpx;
	}

	.square {
		width: 610rpx;
		height: 110rpx;
		background: #3db020;
		border-radius: 20rpx;
		text-align: center;
		line-height: 110rpx;
		font-size: 30rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: Regular;
		text-align: center;
		color: #ffffff;
		margin: 30rpx auto;
		opacity: 0.5;

	}

	/deep/ .u-input {

		background: #fbfbfb !important;

	}

	/deep/.u-form-item__body[data-v-067e4733] {
		padding: 0;
	}

	/deep/ .item1 .u-input--radius {
		border-radius: 8rpx 0 0 8rpx;
	}

	.size {
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: Regular;
		text-align: right;
		color: #999999;
		margin-top: 30rpx;
		margin-bottom: 60rpx;
	}

	/deep/ .u-form-item__body__left__content__required {

		position: absolute;
		left: 26rpx !important;
		top: -14rpx !important;
	}

	/deep/ .u-input {
		height: 60rpx;
	}


	/deep/ .u-input__content__field-wrapper__field {
		line-height: 52rpx;
		text-align: left;
		color: #303133;

		font-size: 30rpx;
		flex: 1;
		padding: 20rpx 40rpx;
	}

	/deep/ .u-upload__button__text[data-v-69e2a36e] {

		color: #909193;
		margin-top: 4rpx;
		text-align: center;
		padding: 0 10rpx;

		height: 72rpx;
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC-Regular;
	}
</style>